﻿using Microsoft.Data.Sqlite;

namespace Sample.ConsoleApp.CodeFist.Sqlite.Utility;

/// <summary>
/// 目录助手
/// </summary>
internal class DirectoryHelper
{
    /// <summary>
    /// 获取当前目录
    /// </summary>
    public string CurrentDirectory => Directory.GetCurrentDirectory();

    /// <summary>
    /// 设置根目录
    /// </summary>
    /// <param name="directory">跟目录名称</param>
    /// <returns></returns>
    public string BuildRootDirectory(string directory) => Path.Combine(CurrentDirectory, directory);

    #region 构建 sqlite 连接字符串
    public string GetSqliteConnectionString(string directory = "Database", string password = "123456")
    {
        // 设置根目录
        string rootDirectory = BuildRootDirectory(directory);
        string sqliteFilePath = Path.Combine(rootDirectory, "test.db");
        //string connectionString = @$"Data Source={ sqliteFilePath };Mode=Memory;Cache=Shared"; //可共享内存数据库
        return BuildSqliteConnectionString(sqliteFilePath, password);
    }

    public string BuildSqliteConnectionString(string filePath, string password)
    {
        var builder = new SqliteConnectionStringBuilder()
        {
            DataSource = filePath,
            Cache = SqliteCacheMode.Shared,
            Mode = SqliteOpenMode.ReadWriteCreate,
            // Password = password, // 此处对应的 nuget 包暂不支持密码设置
            Pooling = true,
            DefaultTimeout = 30,
        };

        return builder.ToString();
    }
    #endregion

}
